package com.zuoyi.test;

import com.zuoyi.dao.IUserDao;
import com.zuoyi.io.Resources;
import com.zuoyi.pojo.User;
import com.zuoyi.sqlSession.SqlSession;
import com.zuoyi.sqlSession.SqlSessionFactory;
import com.zuoyi.sqlSession.SqlSessionFactoryBuilder;
import org.dom4j.DocumentException;
import org.junit.Before;
import org.junit.Test;

import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.util.List;

/**
 * Author: zuo
 * Date: 2021/3/11 17:23
 * Describe:
 */
public class IPersistence {

    private IUserDao userDao = null;
    @Test
    public void test() throws Exception {
        List<User> all = userDao.findAll();
        for (User user : all) {
            System.out.println(user);
        }
        //测试 调用自定义持久层框架
        /*User user = new User();
        user.setId(1);
        user.setAge(18);*/
        /*User user2 = sqlSession.selectOne("user.selectOne", user);
        System.out.println(user2);*/
      /*  List<User> user3 = sqlSession.selectList("user.selectList");
        System.out.println(user3);*/
    }

    @Before
    public void before() throws PropertyVetoException, DocumentException {
        InputStream resourcesAsSteam = Resources.getResourcesAsSteam("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourcesAsSteam);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        userDao = sqlSession.getMapper(IUserDao.class);
    }
    // 增加
    @Test
    public void test3(){
        User user= new User();
        user.setId(6);
        user.setAge(23);
        user.setUsername("zhangsan");
        Integer insert = userDao.insert(user);
        System.out.println(insert);
    }
    //修改
    @Test
    public void test2() throws Exception {

        User user3 = new User();
        user3.setId(6);
        user3.setAge(100);
        user3.setUsername("ceshi");
        int i = userDao.updateById(user3);
        System.out.println(i);
    }
    //删除
    @Test
    public void test4(){
        User user= new User();
        user.setId(6);
        Integer delete = userDao.deleteById(6);
        System.out.println(delete);
    }

}
